package com.justnow;

import jdk.nashorn.internal.ir.CallNode;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;

public class TestSelfAQS {
    private static int a = 0;
    private static int b = 0;
    private static SelfAQS selfAQS = new SelfAQS();
    static ExecutorService ec1 = Executors.newFixedThreadPool(20);
    static ExecutorService ec2 = Executors.newFixedThreadPool(20);

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 20; i++) {
            ec1.submit(new Task());
        }
        for (int i = 0; i < 20; i++) {
            ec1.submit(new TaskSync());
        }
        Thread.sleep(10000);
        System.out.println("a的值：" + a);
        System.out.println("b的值：" + b);
        ec1.shutdown();
        ec2.shutdown();
    }

    /**
     * 未使用自定义同步器的任务
     */
    static class Task implements Runnable {

        @Override
        public void run() {
            for (int i = 0; i < 10000; i++) {
                a++;
            }
        }
    }

    /**
     * 使用自定义同步器的任务
     */
    static class TaskSync implements Runnable {

        @Override
        public void run() {
            for (int i = 0; i < 10000; i++) {
                selfAQS.lock();
                b++;
                selfAQS.unLock();
            }
        }
    }
}
